package com.acme.bankapp.domain.bank;

import java.io.Serializable;

public class Client implements Serializable {
	
	private static final long serialVersionUID = 1L;
	private final static String mr = "Mr.";
	private final static String ms = "Ms.";
	private final static String s0 = "Client: ";
	private final static String s1 = ", gender: ";
	private final static String s2 = ", ";

	private static int maxClientId = 0;
	private static int maxAccountId = 0;
	private int id;

	public Account accountS;
	public Account accountC;
	private String name;
	private Gender gender;

	private enum Gender implements Serializable {
		MALE(mr), FEMALE(ms);

		private String salutate;

		Gender(String salutate) {
			this.salutate = salutate;
		}

		private String getSalutation() {
			return salutate;
		}
	}

	public Client(String name, boolean gender) {
		this.accountS = null;
		this.accountC = null;
		this.name = name;
		this.id = maxClientId;
		maxClientId++;
		if (gender == true) {
			this.gender = Gender.MALE;
		} else {
			this.gender = Gender.FEMALE;
		}
	}

	public void openAccount(final double amountS) {
		this.accountS = new SavingAccount(maxAccountId, amountS);
		maxAccountId++;
	}

	public void openAccount(final double amountC, final double withdraw) {
		this.accountC = new CheckingAccount(maxAccountId, amountC, withdraw);
		maxAccountId++;
	}

	public String getName() {
		return name;
	}

	public String getClientSalutation() {
		return gender.getSalutation() + name;
	}

	@Override
	public int hashCode() {
		return this.id;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (gender != other.gender)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(s0 + name + s1 + gender);
		if (accountS != null) {
			buffer.append(s2 + accountS.toString());
		}
		if (accountC != null) {
			buffer.append(s2 + accountC.toString());
		}
		return buffer.toString();
	}

}
